package com.istudy.servlet;

import com.istudy.entity.CommentsPage;
import com.istudy.entity.Video;
import com.istudy.service.imp.XYJCommentPageImpService;
import com.istudy.service.imp.XYJUserServiceImp;
import com.istudy.service.imp.XYJVideoServiceImpl;
import com.istudy.utils.JsonMessage;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xdd
 * @create 2020-09-03 15:24
 */
@WebServlet("/dispaly.php")
public class DisplayServlet extends AbstractServlet {

    XYJCommentPageImpService commentPageImpService = new XYJCommentPageImpService();//评论逻辑层
    XYJUserServiceImp userServiceImp = new XYJUserServiceImp();//用户逻辑层
    XYJVideoServiceImpl videoServiceImpl = new XYJVideoServiceImpl();//视频逻辑层
    CommentServlet commentServlet=new CommentServlet();
    @Override
    public Class getServletClass() {
        return DisplayServlet.class;
    }


    public String displayPage(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("videos", setVideoList(request, response));// request中添加播放列表
        commentServlet.getCommentByVideo(request,response);
        return "/Displayer";
    }

    /**
     * request中添加播放列表
     *
     * @param request 域对象
     */
    public List<Video> setVideoList(HttpServletRequest request, HttpServletResponse response) {
        List<Video> videos = videoServiceImpl.selectVideoAll(request.getParameter("type"));
        return videos;
    }

    /**
     * request中添加评论分页
     */
    public CommentsPage setCommentpage(HttpServletRequest request, HttpServletResponse response) {
        CommentsPage commentsPage = commentPageImpService.getCommentsPage(request.getParameter("page"));
        return commentsPage;
    }

    /**
     * 播放扣费
     *
     * @param request
     * @return
     */
    public JsonMessage userDeduction(HttpServletRequest request, HttpServletResponse response) {
        String userId = request.getParameter("userId");
        String videoConsume = request.getParameter("videoConsume");
        JsonMessage jsonMessage = new JsonMessage();
        boolean b = userServiceImp.UserDeduction(userId, videoConsume);
        jsonMessage.setDeduction(b);
        return jsonMessage;
    }

}
